home *** CD-ROM | disk | FTP | other *** search
/ PC Graphics Unleashed / PC Graphics Unleashed.iso / ch17 / bas / cubic.bas next >
Encoding:
BASIC Source File  |  1994-08-04  |  6.8 KB  |  205 lines

  1. '=================================================================
  2. '  PROGRAM: CUBIC.BAS
  3. '   AUTHOR: Rob McGregor
  4. '-----------------------------------------------------------------
  5. '  PURPOSE: Creates a Polyray scene file containing 3 spheres
  6. '           that follow different cubic paths, starting and
  7. '           ending at the same points.  The paths are controlled
  8. '           by the variables s1Nc, s2Nc, and s3Nc (the Nc
  9. '           parameter in the equations)
  10. '-----------------------------------------------------------------
  11. '           This is a "virtual animation," in that it calculates
  12. '           the positions of the 3 spheres for each virtual frame,
  13. '           and adjusts the transparency of the spheres based on
  14. '           the value of t.  The resulting image shows the entire
  15. '           animation in only 1 frame.
  16. '=================================================================
  17.  
  18. DECLARE SUB cubic (s1Nc!, s2Nc!, s3Nc!, s1Nd!, s2Nd!, s3Nd!, startF%, endF%, fout$)
  19.  
  20. CLS
  21. PRINT "This program creates a Polyray scene file demonstrating ";
  22. PRINT "cubic blending."
  23. PRINT
  24. PRINT "Please enter a path and file name for the resulting ";
  25. PRINT "Polyray scene file"
  26. LINE INPUT "==>"; fout$
  27.  
  28. INPUT "Slope Nc of sphere 1 (from -10 to 10)"; s1Nc!
  29. INPUT "Slope Nc of sphere 2 (from -10 to 10)"; s2Nc!
  30. INPUT "Slope Nc of sphere 3 (from -10 to 10)"; s3Nc!
  31.  
  32. INPUT "Slope Nd of sphere 1 (from -10 to 10)"; s1Nd!
  33. INPUT "Slope Nd of sphere 2 (from -10 to 10)"; s2Nd!
  34. INPUT "Slope Nd of sphere 3 (from -10 to 10)"; s3Nd!
  35.  
  36. INPUT "Starting frame of the virtual animation"; startF%
  37. INPUT "Ending frame of the virtual animation"; endF%
  38.  
  39. CALL cubic(s1Nc!, s2Nc!, s3Nc!, s1Nd!, s2Nd!, s3Nd!, startF%, endF%, fout$)
  40. PRINT : PRINT "Done!": PRINT
  41.  
  42. SUB cubic (s1Nc!, s2Nc!, s3Nc!, s1Nd!, s2Nd!, s3Nd!, startF%, endF%, fout$)
  43.  
  44.   OPEN fout$ FOR OUTPUT AS #1
  45.  
  46.   ' Declare variables
  47.   DIM frame%, t, i, format$
  48.   DIM N3x!, N3y!, temp$, tex!
  49.   DIM s1x!, s1y!, s2x!, s2y!, s3x!, s3y!
  50.   DIM s1N1x!, s1N1y!, s1N2x!, s1N2y!
  51.   DIM s2N1x!, s2N1y!, s2N2x!, s2N2y!
  52.   DIM s3N1x!, s3N1y!, s3N2x!, s3N2y!
  53.  
  54.   ' Write the file
  55.   PRINT #1, "// Scene File: CUBE.PI"
  56.   PRINT #1, "// Author: Rob McGregor"
  57.   PRINT #1, ""
  58.   PRINT #1, "/***************************************************"
  59.   PRINT #1, " Virtual animation of 3 spheres following different"
  60.   PRINT #1, " cubic paths, each starting and ending at the same"
  61.   PRINT #1, " relative points on the y-axis."
  62.   PRINT #1, "****************************************************/"
  63.   PRINT #1, ""
  64.   PRINT #1, "// SET UP THE CAMERA"
  65.   PRINT #1, "viewpoint {"
  66.   PRINT #1, "  from        <4.5, 0, -11.5>"
  67.   PRINT #1, "  at          <4.5, 1, 0>"
  68.   PRINT #1, "  up          <0, 1, 0>"
  69.   PRINT #1, "  angle       45"
  70.   PRINT #1, "  resolution  320, 240"
  71.   PRINT #1, "  aspect      4/3"
  72.   PRINT #1, "}"
  73.   PRINT #1, ""
  74.   PRINT #1, "background Grey"
  75.   PRINT #1, ""
  76.   PRINT #1, "// Lights"
  77.   PRINT #1, "light <-5, 5, -20>"
  78.   PRINT #1, "light <5, 5, -20>"
  79.   PRINT #1, ""
  80.   
  81.   PRINT #1, "define s1color"
  82.   PRINT #1, "surface { "
  83.   PRINT #1, "  color      red"
  84.   PRINT #1, "  ambient    0.1"
  85.   PRINT #1, "  diffuse    0.2"
  86.   PRINT #1, "  specular   0.8"
  87.   PRINT #1, "  microfacet Cook 0.2"
  88.   PRINT #1, "  reflection 0.3 "
  89.   PRINT #1, "}"
  90.   PRINT #1, ""
  91.   
  92.   PRINT #1, "define s2color"
  93.   PRINT #1, "surface { "
  94.   PRINT #1, "  color      yellow"
  95.   PRINT #1, "  ambient    0.1"
  96.   PRINT #1, "  diffuse    0.2"
  97.   PRINT #1, "  specular   0.8"
  98.   PRINT #1, "  microfacet Cook 0.2"
  99.   PRINT #1, "  reflection 0.3 "
  100.   PRINT #1, "}"
  101.   PRINT #1, ""
  102.   
  103.   PRINT #1, "define s3color"
  104.   PRINT #1, "surface { "
  105.   PRINT #1, "  color      blue"
  106.   PRINT #1, "  ambient    0.1"
  107.   PRINT #1, "  diffuse    0.2"
  108.   PRINT #1, "  specular   0.8"
  109.   PRINT #1, "  microfacet Cook 0.2"
  110.   PRINT #1, "  reflection 0.3 "
  111.   PRINT #1, "}"
  112.   PRINT #1, ""
  113.  
  114.   FOR i% = startF% TO endF%
  115.  
  116.     ' compute t
  117.     frame% = i%
  118.     t = (frame% - startF%) / (endF% - startF%)
  119.  
  120.     ' set the parameters of the equation
  121.     s1N1x! = 0 ' x-axis starting point
  122.     s1N1y! = -1 ' y-axis starting point
  123.     s1N2x! = 9 ' x-axis ending point
  124.     s1N2y! = -1 ' y-axis ending point
  125.  
  126.     s2N1x! = 0 ' x-axis starting point
  127.     s2N1y! = 0 ' y-axis starting point
  128.     s2N2x! = 9 ' x-axis ending point
  129.     s2N2y! = 0 ' y-axis ending point
  130.  
  131.     s3N1x! = 0 ' x-axis starting point
  132.     s3N1y! = 1 ' y-axis starting point
  133.     s3N2x! = 9 ' x-axis ending point
  134.     s3N2y! = 1 ' y-axis ending point
  135.  
  136.     ' Now calculate the locations of the spheres using:
  137.     '   N = (1 - t)^3 * N1 + (t^3) * N2 + 3 * t *
  138.     '       ((1 - t)^2) * Nc + 3 * (t^2) * (1 - t) * Nd
  139.    
  140.     ' use variables to calculate only once
  141.     t2 = t ^ 2
  142.     t3 = t ^ 3
  143.     lessT3 = (1 - t) ^ 3
  144.     lessT2 = (1 - t) ^ 2
  145.  
  146.     s1x! = lessT3 * s1N1x! + t3 * s1N2x! + 3 * t * lessT2 * s1Nc! + 3 * t2 * (1 - t) * s1Nd!
  147.     s1y! = lessT3 * s1N1y! + t3 * s1N2y! + 3 * t * lessT2 * s1Nc! + 3 * t2 * (1 - t) * s1Nd!
  148.     s2x! = lessT3 * s2N1x! + t3 * s2N2x! + 3 * t * lessT2 * s2Nc! + 3 * t2 * (1 - t) * s2Nd!
  149.     s2y! = lessT3 * s2N1y! + t3 * s2N2y! + 3 * t * lessT2 * s2Nc! + 3 * t2 * (1 - t) * s2Nd!
  150.     s3x! = lessT3 * s3N1x! + t3 * s3N2x! + 3 * t * lessT2 * s3Nc! + 3 * t2 * (1 - t) * s3Nd!
  151.     s3y! = lessT3 * s3N1y! + t3 * s3N2y! + 3 * t * lessT2 * s3Nc! + 3 * t2 * (1 - t) * s3Nd!
  152.     
  153.     ' calc the transparency of the spheres
  154.     tex! = (1 - t) * .9 + t * .4
  155.     format$ = "###.###"
  156.     
  157.     line$ = "define s1tex" + LTRIM$(STR$(frame%))
  158.     line$ = line$ + " texture { s1color { transmission white,"
  159.     PRINT #1, line$;
  160.     PRINT #1, USING format$; tex!;
  161.     PRINT #1, ", 1 }}"
  162.     
  163.     line$ = "define s2tex" + LTRIM$(STR$(frame%))
  164.     line$ = line$ + " texture { s2color { transmission white,"
  165.     PRINT #1, line$;
  166.     PRINT #1, USING format$; tex!;
  167.     PRINT #1, ", 1 }}"
  168.     
  169.     line$ = "define s3tex" + LTRIM$(STR$(frame%))
  170.     line$ = line$ + " texture { s3color { transmission white,"
  171.     PRINT #1, line$;
  172.     PRINT #1, USING format$; tex!;
  173.     PRINT #1, ", 1 }}"
  174.     
  175.     PRINT #1, "object { sphere <";
  176.     PRINT #1, USING format$; s1x!;
  177.     PRINT #1, ",";
  178.     PRINT #1, USING format$; s1y!;
  179.     PRINT #1, ", 0>, 0.5";
  180.     line$ = " s1tex" + LTRIM$(STR$(frame%)) + " }"
  181.     PRINT #1, line$
  182.     
  183.     PRINT #1, "object { sphere <";
  184.     PRINT #1, USING format$; s2x!;
  185.     PRINT #1, ",";
  186.     PRINT #1, USING format$; s2y!;
  187.     PRINT #1, ", 0>, 0.5";
  188.     line$ = " s2tex" + LTRIM$(STR$(frame%)) + " }"
  189.     PRINT #1, line$
  190.     
  191.     PRINT #1, "object { sphere <";
  192.     PRINT #1, USING format$; s3x!;
  193.     PRINT #1, ",";
  194.     PRINT #1, USING format$; s3y!;
  195.     PRINT #1, ", 0>, 0.5";
  196.     line$ = " s3tex" + LTRIM$(STR$(frame%)) + " }"
  197.     PRINT #1, line$
  198.     PRINT #1, ""
  199.  
  200.   NEXT i%
  201.   CLOSE #1
  202.  
  203. END SUB
  204.  
  205.